#!/bin/bash

base_url="http://tjj.cq.gov.cn/zwgk_233/fdzdgknr/tjxx/sjzl_55471/tjgb_55472"
list_url=$base_url"/"

year=$1

if [ -z $year ];then
	echo "usage: $0 <year>"
	echo "example: $0 2021"
	exit 1
fi

year_uri=`curl -s -L $list_url | grep -E "${year}年.*国民经济和社会发展统计公报" | awk -F href= '{ print $2 }' | awk '{ print $1 }'|sed 's/"//g' | sed 's/\.//'`

#echo "$year uri: $year_uri"
#exit 0

if [ -z "$year_uri" ];then
	echo "err: parse year uri failed!"
	exit 2
fi

year_url="${base_url}"${year_uri}

echo "$year URL: $year_url"

report=`curl -s -L $year_url`

# dict map
declare -A fields #(["常住人口"]=0, ["生产总值"]=0, ["一般公共预算收入"]=0, ["一般公共预算支出"]=0)

# for sequence
key_list=("常住人口" "生产总值" "一般公共预算收入" "一般公共预算支出")

#for key in ${!fields[*]};
for key in ${key_list[*]};
do
	fields[$key]=`echo $report | awk -F "$key" '{ print $2 }' | awk -F '，' '{ print $1 }' | perl -pe 's/<.*?>//g' | sed 's/\[.*\]//g'`
	#fields[$key]=`echo $report | grep -o -P "${key}[0-9].*?万人|${key}[0-9].*?亿元" | awk -F "$key" '{ print $2 }' | awk -F '，' '{ print $1 }' | perl -pe 's/<.*?>//g' | sed 's/\[.*\]//g'`
	echo "$key: ${fields[$key]}"
done

exit 0

